package com.ies.ieschool.client.module.academic.view.dialog;

import com.ies.ieschool.client.module.academic.view.data.SubjectData;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DragDataAction;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickHandler;
//import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

/**
 * <p>
 * Title: ระบบการจัดการงานโรงเรียน
 * Description: View ในส่วนของ Dialog หลักสูตร
 * Copyright: Copyright (c) 2554
 * Company: Intelligent Enterprise Software
 * @author Sharif Densumite
 * @version IES 2012
 * @since 3-05-2012
 */

public class CourseDialog {
	
	private Window winCourseDetail;
	
	public CourseDialog(RecordDoubleClickEvent event) {
		winCourseDetail = new Window();
		winCourseDetail.setWidth(1200);
		winCourseDetail.setHeight(650);
		winCourseDetail.setTitle("แก้ไขหลักสูตร");
		winCourseDetail.setShowMinimizeButton(false);
		winCourseDetail.setIsModal(true);
		winCourseDetail.setShowModalMask(true);
		winCourseDetail.centerInPage();
		/*winCourseDetail.addCloseClickHandler(new CloseClickHandler() {
			public void onCloseClick(CloseClientEvent event) {
				winCourseDetail.destroy();
			}
		});*/

		Record x = event.getRecord();
		winCourseDetail.addItem(getCourseDetailForm(x.getAttribute("courseName"), x.getAttribute("classRange"), x.getAttribute("criterion")));
		winCourseDetail.show();
	}
	
	public VLayout getCourseDetailForm(String courseName,String classRange,String criterion)
	{
		VLayout vloMainWidget = new VLayout();
		vloMainWidget.setWidth100();
		vloMainWidget.setHeight100();
		vloMainWidget.setMembersMargin(10);
		
		DynamicForm dmfCourForm = new DynamicForm();
		dmfCourForm.setCellPadding(5);
		dmfCourForm.setTitleWidth(100);
		
		TextItem titCourseName = new TextItem();
		titCourseName.setTitle("ชื่อหลักสูตร");
		titCourseName.setValue(courseName);
		titCourseName.setWidth(250);
		
		CanvasItem cviCourseDetailWidget = new CanvasItem();
		cviCourseDetailWidget.setShowTitle(false);
		cviCourseDetailWidget.setColSpan(2);
		
		TabSet tbsCourseDetail = new TabSet();	
		tbsCourseDetail.setHeight(550);
		tbsCourseDetail.setWidth(1000);
		
		cviCourseDetailWidget.setCanvas(tbsCourseDetail);
			
		//=========================================== Tab ข้อมูลทั่วไป ======================================================
		Tab tabCourseGeneralDetail = new Tab("ข้อมูลทั่วไป");
		
		DynamicForm dmfCourseGeneralDetailForm = new DynamicForm();
		dmfCourseGeneralDetailForm.setNumCols(8);
		dmfCourseGeneralDetailForm.setTitle("*");
		dmfCourseGeneralDetailForm.setCellPadding(5);
			
		dmfCourForm.setFields(titCourseName,cviCourseDetailWidget);
		vloMainWidget.addMember(dmfCourForm);

		ComboBoxItem cbiCourseType = new ComboBoxItem();
		cbiCourseType.setTitle("ประเภท");
		cbiCourseType.setType("comboBox");
		cbiCourseType.setValueMap("การศึกษาขั้นพื้นฐาน", "อาชีวศึกษา", "อื่น ๆ");
		

		ComboBoxItem cbiClassRange = new ComboBoxItem();
		cbiClassRange.setTitle("ระดับช่วงชั้น");
		cbiClassRange.setType("comboBox");
		cbiClassRange.setValueMap("ประถมต้น", "ประถมปลาย", "มัธยมต้น");
		cbiClassRange.setValue(classRange);
		
		ComboBoxItem cbiCourseCriterion = new ComboBoxItem();
		cbiCourseCriterion.setTitle("เกณฑ์");
		cbiCourseCriterion.setType("comboBox");
		cbiCourseCriterion.setValueMap("น้ำหนัก", "หน่วยกิต", "ชั่วโมง");
		cbiCourseCriterion.setValue(criterion);
		
		TextItem titCourseBeginingYear = new TextItem();
		titCourseBeginingYear.setTitle("ปีที่เริ่มใช้");
		
		
		TextAreaItem taiCourseGraduateCriterion = new TextAreaItem();
		taiCourseGraduateCriterion.setTitle("เกณฑ์การจบ");
		taiCourseGraduateCriterion.setTitleVAlign(VerticalAlignment.TOP);
		taiCourseGraduateCriterion.setWidth(915);
		taiCourseGraduateCriterion.setHeight(200);
		taiCourseGraduateCriterion.setColSpan(7);
		
		TextAreaItem taiCourseSchoolRecordLevel = new TextAreaItem();
		taiCourseSchoolRecordLevel.setTitle("ระดับผลการเรียน");
		taiCourseSchoolRecordLevel.setTitleVAlign(VerticalAlignment.TOP);
		taiCourseSchoolRecordLevel.setWidth(370);
		taiCourseSchoolRecordLevel.setHeight(200);
		taiCourseSchoolRecordLevel.setColSpan(3);
		
		TextAreaItem taiCourseQualityMeasurement = new TextAreaItem();
		taiCourseQualityMeasurement.setTitle("คุณภาพการเรียน");
		taiCourseQualityMeasurement.setTitleVAlign(VerticalAlignment.TOP);
		taiCourseQualityMeasurement.setWidth(370);
		taiCourseQualityMeasurement.setHeight(200);
		taiCourseQualityMeasurement.setColSpan(3);
		
		TextItem titCourseNote = new TextItem();		
		titCourseNote.setTitle("หมายเหตุ");	
		titCourseNote.setWidth(170);
		
		TextItem titCourseDegreeName = new TextItem();
		titCourseDegreeName.setTitle("ชื่อวุฒิ");
		
		TextItem titCourseDegreeNameAbbreviation = new TextItem();
		titCourseDegreeNameAbbreviation.setTitle("ชื่อชื่อยอ");


		dmfCourseGeneralDetailForm.setItems(
				cbiCourseType,cbiClassRange,cbiCourseCriterion,
				titCourseBeginingYear,taiCourseGraduateCriterion,taiCourseSchoolRecordLevel,
				taiCourseQualityMeasurement,titCourseNote,titCourseDegreeName,titCourseDegreeNameAbbreviation);

		tabCourseGeneralDetail.setPane(dmfCourseGeneralDetailForm);
		tbsCourseDetail.addTab(tabCourseGeneralDetail);
		
		//=========================================== Tab รายวิชา ======================================================
		
		Tab tabCourseSubject = new Tab("รายวิชา");
		
		HLayout hloCourseSubjectWidget = new HLayout();
		hloCourseSubjectWidget.setMembersMargin(5);
		
		VLayout vloCourseSubjectAllWidget = new VLayout();
		vloCourseSubjectAllWidget.setShowResizeBar(true); 
		vloCourseSubjectAllWidget.setResizeBarSize(3);
		
		Label lblCourseSubjectAllHead = new Label("<font size=\"2\"><b>รายวิชาวิชาทั้งหมด</b></font>");
		lblCourseSubjectAllHead.setHeight("26px");
		
		vloCourseSubjectAllWidget.addMember(lblCourseSubjectAllHead);
		
		//field ในตารางของ วิชาทั้งหมด
		ListGrid lsgCourseSubjectAllList = new ListGrid();
		lsgCourseSubjectAllList.setShowAllRecords(true);  
		lsgCourseSubjectAllList.setCanDragRecordsOut(true);  
		lsgCourseSubjectAllList.setCanAcceptDroppedRecords(true);  
		lsgCourseSubjectAllList.setDragDataAction(DragDataAction.MOVE);  
		//field ในตารางของ วิชาที่ถูกเลือก
		ListGridField lsgfCourseSubjectID = new ListGridField("subject_id", "รหัสวิชา");
		ListGridField lsgfCourseSubjectName = new ListGridField("subject_name", "ชื่อวิชา");
		ListGridField lsgfCourseSubjectLearningType = new ListGridField("learning_type", "สาระการเรียนรู้");
		ListGridField lsgfCourseSubjectWeight = new ListGridField("subject_weight", "น้ำหนัก");
		lsgfCourseSubjectWeight.setCellAlign(Alignment.RIGHT);
		ListGridField lsgfCourseSubjectHour = new ListGridField("subject_hours", "ชั่วโมงเรียน");
		lsgfCourseSubjectHour.setCellAlign(Alignment.RIGHT);
		
		lsgCourseSubjectAllList.setFields(lsgfCourseSubjectID,lsgfCourseSubjectName,lsgfCourseSubjectLearningType,lsgfCourseSubjectWeight,lsgfCourseSubjectHour);
		lsgCourseSubjectAllList.setData(SubjectData.getRecords());
		
		vloCourseSubjectAllWidget.addMember(lsgCourseSubjectAllList);
		hloCourseSubjectWidget.addMember(vloCourseSubjectAllWidget);
		
		VLayout vloCourseSubjectChoosedWidget = new VLayout();
		
		Label lblCourseSubjectChoosedHead = new Label("<font size=\"2\"><b>รายวิชาที่เลือก</b></font>");
		lblCourseSubjectChoosedHead.setHeight("25px");
		
		vloCourseSubjectChoosedWidget.addMember(lblCourseSubjectChoosedHead);
		
		ListGrid lsgCourseSubjectChoosedList = new ListGrid();
		lsgCourseSubjectChoosedList.setFields(lsgfCourseSubjectID,lsgfCourseSubjectName,lsgfCourseSubjectLearningType,lsgfCourseSubjectWeight,lsgfCourseSubjectHour);
		lsgCourseSubjectChoosedList.setShowAllRecords(true);  
		lsgCourseSubjectChoosedList.setEmptyMessage("ลากวิชาที่ต้องการที่นี่!!");  
		lsgCourseSubjectChoosedList.setCanReorderFields(true);  
		lsgCourseSubjectChoosedList.setCanDragRecordsOut(true);  
		lsgCourseSubjectChoosedList.setCanAcceptDroppedRecords(true);  
		lsgCourseSubjectChoosedList.setDragDataAction(DragDataAction.MOVE); 
		
		vloCourseSubjectChoosedWidget.addMember(lsgCourseSubjectChoosedList);
		hloCourseSubjectWidget.addMember(vloCourseSubjectChoosedWidget);		
		tabCourseSubject.setPane(hloCourseSubjectWidget);	
		tbsCourseDetail.addTab(tabCourseSubject);	
		
		IButton ibtnSave = new IButton("บันทึก");
		ibtnSave.addClickHandler(new ClickHandler() {  
            public void onClick(ClickEvent event) {    
            	SC.say("บันทึกสำเร็จ");
            	winCourseDetail.destroy();
            }  
        });  
		
		vloMainWidget.addMember(ibtnSave);
		
		return vloMainWidget;
	}
}